package com.yangxk.kadmin.web.module.system.controller;

import com.yangxk.kadmin.common.beans.Page;
import com.yangxk.kadmin.common.beans.Result;
import com.yangxk.kadmin.web.module.system.form.RoleAuthForm;
import com.yangxk.kadmin.web.module.system.form.RoleDeptForm;
import com.yangxk.kadmin.web.module.system.form.RoleEditForm;
import com.yangxk.kadmin.web.module.system.form.RoleQueryForm;
import com.yangxk.kadmin.web.module.system.po.Role;
import com.yangxk.kadmin.web.module.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/role/list")
    public Result<Page> queryByPage(RoleQueryForm roleQueryForm) {
        return Result.ofSuccess(roleService.queryByPage(roleQueryForm));
    }

    /**
     * 添加角色
     * create by yangxk on 2018/10/16 16:36
     */
    @PostMapping("/role/add")
    public Result add(@Valid @RequestBody RoleEditForm form) {
        roleService.addRole(form);
        return Result.ofSuccess();
    }

    /**
     *  根据ID查询
     * create by yangxk 2018/10/16 20:30
     */
    @GetMapping("/role/detail")
    public Result<Role> queryById(long id) {
        Role role = roleService.selectByPrimaryKey(id);
        return Result.ofSuccess(role);
    }

    /**
     * 修改角色
     * create by yangxk on 2018/10/16 16:37
     */
    @PostMapping("/role/update")
    public Result update(@Valid @RequestBody RoleEditForm form) {
        roleService.updateRole(form);
        return Result.ofSuccess();
    }

    /**
     * 删除角色
     * create by yangxk on 2018/10/16 16:37
     */
    @GetMapping("/role/delete")
    public Result delete(long id) {
        roleService.deleteRole(id);
        return Result.ofSuccess();
    }

    /**
     * 分配权限
     * create by yangxk 2018/10/14 14:40
     */
    @PostMapping("/role/setAuth")
    public Result setAuth(@Valid @RequestBody RoleAuthForm ra) {
        roleService.setAuth(ra.getRoleId(), ra.getAuthIds());
        return Result.ofSuccess();
    }

    /**
     * 查询角色权限ID集合
     * create by yangxk 2018/10/14 15:18
     */
    @GetMapping("/role/auth")
    public Result roleAuth(long roleId) {
        return Result.ofSuccess(roleService.findRoleAuth(roleId));
    }

    /**
     * 查询角色的数据权限(deptId)
     * create by yangxk on 2019/2/2 17:47
     */
    @GetMapping("/role/dept")
    public Result roleDept(long roleId) {
        return Result.ofSuccess(roleService.findRoleDept(roleId));
    }

    /**
     * 为角色设置数据权限(deptId)
     * create by yangxk on 2019/2/2 17:50
     */
    @PostMapping("/role/setDept")
    public Result setDept(@Valid @RequestBody RoleDeptForm rd) {
        roleService.setDept(rd.getRoleId(), rd.getDeptIds());
        return Result.ofSuccess();
    }
}
